#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Text.RegularExpressions;

#endregion

namespace TVC.Modules.CustomControl
{
    public partial class DateTimeCustom : UserControl
    {
        string partentDate = @"^(((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00|[048])))$";
        public DateTimeCustom()
        {
            InitializeComponent();
            txtDate.TextChanged += new EventHandler(txtDate_TextChanged);
        }
        bool _isCal = true;


        void txtDate_TextChanged(object sender, EventArgs e)
        {
            CheckDate(txtDate.Text);
            if (OnDateChange != null) OnDateChange(this, e);
        }

        public override string Text
        {
            get
            {
                return txtDate.Text;
            }
            set
            {
                if (value == null) return;
                if (value.Length != 10 && value.Length != 0) return;
                CheckDate(value);

            }
        }

        [PropertyTab]
        public bool HideButton
        {
            get { return dtpCustom.Visible; }
            set { dtpCustom.Visible = value; }
        }


        public override bool Enabled
        {
            get
            {
                return base.Enabled;
            }
            set
            {
                txtDate.Enabled = base.Enabled = value;
            }
        }

        public override bool Focused
        {
            get
            {
                return txtDate.Focused;
            }
        }

        public event EventHandler OnDateChange;
        private void dtpCustom_ValueChanged(object sender, EventArgs e)
        {
            if (_isCal)
            {
                _isCal = false;
                txtDate.Text = dtpCustom.Value.ToString("dd/MM/yyyy");
                _isCal = true;
            }
        }

        public DateTime Value
        {
            get
            {
                try
                {
                    string[] arr = txtDate.Text.Split('/');
                    if (arr.Length == 3) return new DateTime(Convert.ToInt32(arr[2]), Convert.ToInt32(arr[1]), Convert.ToInt32(arr[0]));
                    return DateTime.MinValue;
                }
                catch { return DateTime.MinValue; }
            }
            set
            {

                _isCal = false;
                if (value == DateTime.MinValue)
                {
                    txtDate.Text = "";
                    dtpCustom.Value = DateTime.Today;
                }
                else if (txtDate.Text != value.ToString("dd/MM/yyyy"))
                {
                    txtDate.Text = value.ToString("dd/MM/yyyy");
                    dtpCustom.Value = value;
                }

                _isCal = true;

            }

        }
        private void CheckDate(string strDate)
        {
            if (_isCal)
            {
                _isCal = false;
                try
                {
                    int err = 0;
                    int day = 0;
                    int month = 0;
                    int year = 0;
                    string[] arr = null;
                    if (strDate == "")
                    {
                        _isCal = true;
                        return;
                    }
                    if (Regex.IsMatch(strDate, partentDate)) err = 0;
                    else err = 1;

                    if (err == 0)
                    {
                        arr = strDate.Split('/');
                        if (arr.Length != 3) arr = strDate.Split('-');
                        if (arr.Length != 3) arr = strDate.Split('.');
                        if (arr.Length != 3) err = 1;
                    }

                    day = Convert.ToInt32(arr[0]);
                    month = Convert.ToInt32(arr[1]);
                    string strYear = DateTime.Today.Year / 100 + arr[2];
                    year = Convert.ToInt32(strYear.Substring(strYear.Length - 4, 4));

                    string val = string.Format("{0}/{1}/{2}", day, month, year);
                    if (txtDate.Text != val) txtDate.Text = val;

                    dtpCustom.Value = new DateTime(year, month, day);

                    if (err != 0)
                    {
                        txtDate.Text = "";
                        dtpCustom.Value = DateTime.Today;
                        _isCal = true;
                        return;// false;
                    }
                }
                catch (Exception ex)
                {
                    txtDate.Text = "";
                    dtpCustom.Value = DateTime.Today;

                }
                finally { _isCal = true; }
            }
        }

        private void txtDate_GotFocus(object sender, EventArgs e)
        {
            this.Focus();
        }

        private void dtpCustom_Click(object sender, EventArgs e)
        {
            if (_isCal)
            {
                _isCal = false;
                if (txtDate.Text == "")
                    if (txtDate.Text != dtpCustom.Value.ToString("dd/MM/yyyy"))
                        txtDate.Text = dtpCustom.Value.ToString("dd/MM/yyyy");
                _isCal = true;
            }

        }

        private void dtpCustom_GotFocus(object sender, EventArgs e)
        {
            txtDate.Focus();
        }
    }
}